
# Main File
# Author: Rishav Jain

import profile_io.device as Device
import profile_io.sensoract as Sensoract
import profile_io.connection as Connection
import profile_io.log as LogParam

FILE_PATH = './profile_io/data/'

DEVICE_PARAMETERS_FILE = 'device.conf'
SENSORACT_PARAMETERS_FILE = 'sensoract.conf'
CONNECTION_PARAMETERS_FILE = 'connection.conf'
LOG_PARAMETERS_FILE = 'log.conf'

class PROFILE:
		def __init__(self):
		## DEVICE PARAMETERS ##
				self.Mode = 2   # 1 for Real-time Sampling/Publish
										# 2 for Store & Upload

				self.SamplingPeriod = 1 # time Period to sample readings (in sec)

				##############################
				# in case of
				# Real-time Sampling/Publish
				#
				self.PublishPeriod = 10
				##############################

				##############################
				# in case of
				# Store & Upload
				#
				self.StoreLength = 10
				self.StorePath = './readings/'
				self.UploadPeriod = 20
				##############################

				## SENSORACT PARAMETERS ##
				self.APIkey = '3773bd8cf9594ca7a2a6c0074f73ace7'
				self.DeviceName = 'RPi-RJ'
				self.DeviceLocation = 'RJ-Home'

				## CONNECTION PARAMETERS ##
				self.ServerIP = 'sensoract.iiitd.edu.in'
#			   self.ServerIP = '192.168.1.2'
				self.ServerPort = 9000
				self.ServerURL = '/upload/wavesegment'

				## LOG PARAMETERS ##
				self.EnableLog = True
				self.LogDir = './logs'

		def saveProfile(self):
				PARAM = {}

				PARAM = {'Mode':self.Mode,
						 'SamplingPeriod':self.SamplingPeriod,
						 'PublishPeriod':self.PublishPeriod,
						 'StoreLength':self.StoreLength,
						 'StorePath':self.StorePath,
						 'UploadPeriod':self.UploadPeriod}

				Device.save(FILE_PATH + DEVICE_PARAMETERS_FILE, PARAM)

				PARAM = {'APIkey':self.APIkey,
						 'DeviceName':self.DeviceName,
						 'DeviceLocation':self.DeviceLocation}

				Sensoract.save(FILE_PATH + SENSORACT_PARAMETERS_FILE, PARAM)

				PARAM = {'ServerIP':self.ServerIP,
						 'ServerPort':self.ServerPort,
						 'ServerURL':self.ServerURL}

				Connection.save(FILE_PATH + CONNECTION_PARAMETERS_FILE, PARAM)

				PARAM = {'EnableLog':self.EnableLog,
						 'LogDir':self.LogDir}

				LogParam.save(FILE_PATH + LOG_PARAMETERS_FILE, PARAM)

				return

		def loadProfile(self):
				PARAM = Device.load(FILE_PATH + DEVICE_PARAMETERS_FILE)
				self.Mode = PARAM['Mode']
				self.SamplingPeriod = PARAM['SamplingPeriod']
				self.PublishPeriod = PARAM['PublishPeriod']
				self.StoreLength = PARAM['StoreLength']
				self.StorePath = PARAM['StorePath']
				self.UploadPeriod = PARAM['UploadPeriod']

				PARAM = Sensoract.load(FILE_PATH + SENSORACT_PARAMETERS_FILE)
				self.APIkey = PARAM['APIkey']
				self.DeviceName = PARAM['DeviceName']
				self.DeviceLocation = PARAM['DeviceLocation']

				PARAM = Connection.load(FILE_PATH + CONNECTION_PARAMETERS_FILE)
				self.ServerIP = PARAM['ServerIP']
				self.ServerPort = PARAM['ServerPort']
				self.ServerURL = PARAM['ServerURL']

				PARAM = LogParam.load(FILE_PATH + LOG_PARAMETERS_FILE)

				self.EnableLog = PARAM['EnableLog']
				self.LogDir = PARAM['LogDir']

				return

		def profileInit(self):
				try:
						self.loadProfile()
				except Exception as e:
						print('Error Loading Profile -')
						print(e.args)
						print('Loading Default Values')
						self.__init__()
						self.saveProfile()
				return

		def printProfile(self):
				print 'Mode', self.Mode
				print 'SamplingPeriod', self.SamplingPeriod
				print 'PublishPeriod', self.PublishPeriod
				print 'StoreLength', self.StoreLength
				print 'StorePath', self.StorePath
				print 'UploadPeriod', self.UploadPeriod
				print 'APIkey', self.APIkey
				print 'DeviceName', self.DeviceName
				print 'DeviceLocation', self.DeviceLocation
				print 'ServerIP', self.ServerIP
				print 'SeverPort', self.ServerPort
				print 'ServerURL', self.ServerURL
				print 'EnableLog', self.EnableLog
				print 'LogDir', self.LogDir

profile = PROFILE()

###
### FOR TESTING PURPOSES ONLY
###
#~ profile.profileInit()
#~ 
#~ profile.printProfile()
